package com.aaa.rabbitmq;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

// 生产者(发布消息) -->  rabbitmq(存贮消息)  --> 消费者(获取消息，处理消息)
// exchange交换机  queue队列
@Configuration
public class RabbitMQConfig {

    /**
     * 活动队列--(添加死信交换机,消息过期后进入)-->死信交换机--(发布)-->死信队列---(消费)-->监听
     */
    public static final String PMS_PRODUCT_QUEUE = "product-queue";


    // 活动队列
    public static final String DEAD_PMS_PRODUCT_QUEUE = "dead-product-queue";
    // 死信交换机
    public static final String DEAD_EXCHANGE = "my-direct-ex";

//@Bean
//public Queue miaoshaQueue(){
//    return new Queue(DEAD_EXCHANGE,true);
//}
    /**
     * 声明direct交换机
     *
     * @return
     */
    @Bean("deadExchange")
    public FanoutExchange createExchange() {
        FanoutExchange fanoutExchange = new FanoutExchange(DEAD_EXCHANGE, true, false);
        return fanoutExchange;
    }
    /**
     * 创建死信队列
     *
     * @return
     */
    @Bean("deadQueue")
    public Queue createDirectQueue() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-max-length", 50);
        map.put("x-overflow", "drop-head");
        Queue queue = new Queue(DEAD_PMS_PRODUCT_QUEUE, true, false, false, map);
        return queue;
    }
    /**
     * 创建活动队列
     *
     * @return
     */
    @Bean("pmsproductqueue")
    public Queue createApmsproductqueue() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("x-max-length", 50);
        map.put("x-overflow", "drop-head");//溢出
        map.put("x-dead-letter-exchange",DEAD_EXCHANGE);
        return new Queue(PMS_PRODUCT_QUEUE,true,false,false,map);
    }



    @Bean
    public Binding binding(@Qualifier("deadQueue") Queue queue, @Qualifier("deadExchange") FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

}
